package com.xxx.client.android.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.xxx.client.android.R;
import com.xxx.client.android.CustomApplication;
import com.xxx.client.android.adapter.PurpleAdapter;
import com.xxx.client.android.adapter.PurpleEntry;
import com.xxx.client.android.adapter.PurpleListener;
import com.xxx.client.android.adapter.SeparatedListAdapter;
import com.xxx.client.android.api.ArticleListEntry;
import com.xxx.client.android.api.ArticleVO;
import com.xxx.client.android.api.util.Ksoap2Caller;
import com.xxx.client.android.dialog.ArticleListLoadingDialog;
import com.xxx.client.android.util.Tool;

public class ArticleListActivity extends Activity {

	private ArticleListEntry articleList;
	private ListView mArticleListView;

	private Integer submenuId;
	private static String[] params;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		mArticleListView = (ListView) findViewById(R.id.Listview);
	}

	@Override
	protected void onResume() {
		fillArticleListView();
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// support for search key
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			if (submenuId != null) {
				SearchActivity.launch(ArticleListActivity.this, submenuId);
			}
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about_menu_item:
			Tool.about(this);
			break;
		case R.id.refresh_menu_item:
			refresh();
			break;
		default:
		}
		return super.onOptionsItemSelected(item);
	}

	public void refresh() {
		Ksoap2Caller.clearCache();
		new ArticleListLoadingDialog(this, R.string.article_list_loading,
				R.string.article_list_fail).execute(params);
	}

	/**
	 * Launch this Activity from the outside
	 * 
	 * @param c
	 *            Activity from which ArticleListActivity should be started
	 * @param params
	 *            params[0]:keyword params[1]:type params[2]:submenuId
	 *            params[3]:submenuName
	 */
	public static void launch(Activity c, String[] params) {
		ArticleListActivity.params = params;
		new ArticleListLoadingDialog(c, R.string.article_list_loading,
				R.string.article_list_fail).execute(params);
	}

	/**
	 * Fills ListView with clickable menu items
	 */
	private void fillArticleListView() {
		CustomApplication.log("ArticleListActivity.fillArticleListView");

		articleList = (ArticleListEntry) getIntent().getSerializableExtra(
				"articleList");
		submenuId = articleList.getHeaderId();

		// separate adapters on one list
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		PurpleAdapter mPurpleAdapter = new PurpleAdapter(this);
		ArrayList<PurpleEntry> mListEntry = new ArrayList<PurpleEntry>();
		ArticleVO articleVO;
		int count = articleList.getCount();
		for (int i = 0; i < count; i++) {
			articleVO = articleList.getArticle(i);
			final Integer articleId = articleVO.getArticleId();
			mListEntry.add(new PurpleEntry(articleVO.getTitleName(),
					new PurpleListener() {
						@Override
						public void performAction() {
							ArticleActivity.launch(ArticleListActivity.this,
									new Integer[] { articleId, null, 1 });
						}
					}));
		}
		// attach list data to adapters
		mPurpleAdapter.setList(mListEntry);
		// separate adapters on one list
		separatedAdapter.addSection(articleList.getHeader(), mPurpleAdapter);

		mArticleListView.setAdapter(separatedAdapter);
		mArticleListView.setOnItemClickListener(mArticleClickListener);
	}

	/**
	 * Launches menu actions
	 */
	private OnItemClickListener mArticleClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view,
				int index, long time) {
			PurpleListener listener = ((PurpleEntry) adapterView.getAdapter()
					.getItem(index)).getListener();
			if (listener != null) {
				listener.performAction();
			}
		}

	};

}
